﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
namespace BusinessLogicTier.Connection
{
    public static class Utilities
    {
        public static String DatabaseName;
        #region Các hàm xử lý tập tin XML
        public class XML
        {
            
            public static XmlDocument XMLReader(String filename)
            {
                XmlDocument xmlR = new XmlDocument();
                try
                {
                    xmlR.Load(filename);
                }
                catch
                {
                    MessageBox.Show("Không đọc được hoặc không tồn tại tập tin cấu hình " + filename, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                return xmlR;
            }

            public static void XMLWriter(String filename, String servname, String database, String costatus)
            {
                XmlTextWriter xmlW = new XmlTextWriter(filename, null);
                xmlW.Formatting = Formatting.Indented;

                xmlW.WriteStartDocument();
                xmlW.WriteComment("\nKhong duoc thay doi noi dung file nay!\n" +
                                    "Thong so co ban:\n\t" +
                                    "costatus = true : quyen Windows\n\t" +
                                    "costatus = false: quyen SQL Server\n\t" +
                                    "servname: ten server\n\t" +
                                    "username: ten dang nhap he thong\n\t" +
                                    "password: mat khau dang nhap he thong\n\t" +
                                    "database: ten co so du lieu\n");
                xmlW.WriteStartElement("config");

                xmlW.WriteStartElement("costatus");
                xmlW.WriteString(costatus);
                xmlW.WriteEndElement();

                xmlW.WriteStartElement("servname");
                xmlW.WriteString(servname);
                xmlW.WriteEndElement();

                xmlW.WriteStartElement("username");
                xmlW.WriteString("");
                xmlW.WriteEndElement();

                xmlW.WriteStartElement("password");
                xmlW.WriteString("");
                xmlW.WriteEndElement();

                xmlW.WriteStartElement("database");
                xmlW.WriteString(database);
                xmlW.WriteEndElement();

                xmlW.WriteEndElement();
                xmlW.WriteEndDocument();

                xmlW.Close();
            }

            public static void XMLWriter(String filename, String servname, String username, String password, String database, String costatus)
            {
                XmlTextWriter xmlW = new XmlTextWriter(filename, null);
                xmlW.Formatting = Formatting.Indented;

                xmlW.WriteStartDocument();
                xmlW.WriteComment("\nKhong duoc thay doi noi dung file nay!\n" +
                                    "Thong so co ban:\n\t" +
                                    "costatus = true : quyen Windows\n\t" +
                                    "costatus = false: quyen SQL Server\n\t" +
                                    "servname: ten server\n\t" +
                                    "username: ten dang nhap he thong\n\t" +
                                    "password: mat khau dang nhap he thong\n\t" +
                                    "database: ten co so du lieu\n");
                xmlW.WriteStartElement("config");

                xmlW.WriteStartElement("costatus");
                xmlW.WriteString(costatus);
                xmlW.WriteEndElement();

                xmlW.WriteStartElement("servname");
                xmlW.WriteString(servname);
                xmlW.WriteEndElement();

                xmlW.WriteStartElement("username");
                xmlW.WriteString(username);
                xmlW.WriteEndElement();

                xmlW.WriteStartElement("password");
                xmlW.WriteString(password);
                xmlW.WriteEndElement();

                xmlW.WriteStartElement("database");
                xmlW.WriteString(database);
                xmlW.WriteEndElement();

                xmlW.WriteEndElement();
                xmlW.WriteEndDocument();

                xmlW.Close();
            }
        }
        #endregion
    }
}
